{extend name="../../index/view/public/layout" /}
{block name="style"}
<link rel="stylesheet" href="__STATIC__/member/css/member.css">
{/block}

{block name="main-container"}
<div class="member-container wrapper clearfix">
    {include file="public/left" /}
    <div class="member-main">
        <div class="member-main-title clearfix">
            <h3> 个人资料 > 修改密码 </h3>
        </div>
        <form action="{:url('password')}" class="form form-telephone" method="post">

            <div class="alert alert-warning">
                密码长度为 6 - 16位，必须包含数字、拼音
            </div>
            <div class="control-group">
                <label for="">原密码</label>
                <div class="con">
                    <input type="password" name="oldpwd" class="" id="oldpwd">
                </div>
            </div>
            <div class="control-group">
                <label for="">新密码</label>
                <div class="con">
                    <input type="password" name="newpwd" class="" id="newpwd">
                </div>
            </div>
            <div class="control-group">
                <label for="">确认密码</label>
                <div class="con">
                    <input type="password" name="subpwd" class="" id="subpwd">
                </div>
            </div>
            <div class="control-group">
                <label for=""></label>
                <div class="con">
                    <input type="submit" class="btn btn-primary btn-red btn-submit" value="确认添加"/>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="script"}
<script src="__STATIC__/libs/layer/layer.js"></script>
<script src="__STATIC__/libs/jquery-validation/jquery.validate.js"></script>
<script src="__STATIC__/libs/jquery-validation/jquery.validate.messages_zh.js"></script>
<script>
    $.validator.addMethod("pass_strength", function (value, element) {
        return this.optional(element) || /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]{6,16}$/.test(value);
    }, "密码长度为6-16位，必须包含数字、字母");
    (function () {
        $(".form-telephone").validate({
            rules: {
                oldpwd: {
                    required: true,
                    // pass_strength: true //暂时不加校验，以免测试用户无法使用
                },
                newpwd: {
                    required: true,
                    pass_strength: true
                },
                subpwd: {
                    equalTo: "#newpwd"
                }
            },
            messages: {
                oldpwd: {
                    required: "请输入旧密码",
                },
                newpwd: {
                    required: "请输入密码",
                },
                subpwd: {
                    equalTo: "密码不一致"
                }
            },
            submitHandler: function () {
                $.ajax({
                    url: "{:url('password')}",
                    data: {
                        oldpwd: $("#oldpwd").val(),
                        newpwd: $("#newpwd").val(),
                        subpwd: $("#subpwd").val()
                    },
                    type: "post",
                    dataType: 'json',
                    success: function (data) {
                        if (data.code === 1) {
                            layer.msg(data.msg, function () {
                                window.location.href = data.url
                            })
                        } else {
                            layer.msg(data.msg)
                        }
                    }
                })
            },
            errorElement: "span",
            errorPlacement: function (error, element) {
                $(element).closest(".control-group").append(error);
            }
        })
    }())
</script>
{/block}
